package com.alinesno.cloud.portal.auto.controller;

import java.io.File;
import java.io.IOException;
import java.util.Collections;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.jenkinsci.remoting.util.Charsets;
import org.kohsuke.stapler.RequestImpl;
import org.kohsuke.stapler.ResponseImpl;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alinesno.cloud.common.core.constants.SpringInstanceScope;
import com.alinesno.cloud.common.facade.pageable.DatatablesPageBean;
import com.alinesno.cloud.common.web.base.advice.TranslateCode;
import com.alinesno.cloud.common.web.base.controller.FeignMethodController;
import com.alinesno.cloud.portal.auto.constants.CommonPath;
import com.alinesno.cloud.portal.auto.entity.AutoTaskEntity;
import com.alinesno.cloud.portal.auto.entity.AutoTaskHistoryEntity;
import com.alinesno.cloud.portal.auto.service.IAutoTaskHistoryService;
import com.alinesno.cloud.portal.auto.service.IAutoTaskService;

import hudson.console.AnnotatedLargeText;

/**
 * <p> 前端控制器 </p>
 *
 * @author WeiXiaoJin
 * @since 2019-11-22 18:53:18
 */
@Controller
@Scope(SpringInstanceScope.PROTOTYPE)
@RequestMapping("portal/auto/autoTaskHistory")
public class AutoTaskHistoryController extends FeignMethodController<AutoTaskHistoryEntity, IAutoTaskHistoryService> {

	//日志记录
	private static final Logger log = LoggerFactory.getLogger(AutoTaskHistoryController.class);

	@Autowired
	private IAutoTaskHistoryService autoTaskHistoryService; 
	
	@Autowired
	private IAutoTaskService autoTaskService; 

	private Stapler stapler = new Stapler() ; 

	@TranslateCode(value="[{hasStatus:has_status}]")
	@ResponseBody
	@PostMapping("/datatables")
    public DatatablesPageBean datatables(HttpServletRequest request , Model model ,  DatatablesPageBean page){
		log.debug("page = {}" , ToStringBuilder.reflectionToString(page));
		return this.toPage(model, this.getFeign() , page) ;
    }

	@Override
	public IAutoTaskHistoryService getFeign() {
		return this.autoTaskHistoryService;
	}

	/**
	 * 显示日志
	 * @return 
	 * @throws IOException 
	 */
	@ResponseBody
	@PostMapping("showLogger")
	public String showLogger(
			@NotBlank(message = "日志位置不能为空.") int start , 
			@NotBlank(message = "任务id不能为空.") String taskId) throws IOException {
		log.debug("task id:{} , start:{}" , taskId , start);
		
		AutoTaskEntity e = autoTaskService.findById(taskId).get() ; 

		File f = new File(CommonPath.autoHome() + File.separator + e.getId()+File.separator+e.getTaskVersion()+File.separator+"run.log") ; 
		AnnotatedLargeText<Void> text = new AnnotatedLargeText<Void>(f, Charsets.UTF_8 , false , null) ; 

		StaplerRequest req = new RequestImpl(stapler, request, Collections.emptyList() , null); 
		StaplerResponse rsp = new ResponseImpl(stapler, response); 
	
		text.doProgressiveHtml(req , rsp);  
	
		return null ; 
	}

}


























